package org.chiv.gof.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 观察者模式
 *
 */
public class ObserverModel {

    static abstract class Subject{
        protected List<Observer> list = new ArrayList<Observer>();

        protected void add(Observer observer){
            list.add(observer);
        }

        //通知所以观察者
        abstract void notifyAllOb();


    }
    interface Observer{
        void response();
    }
    static class ObserverA implements  Observer{

        public void response() {
            System.out.println("ObserverA 收到了");
        }
    }
    static class ObserverB implements  Observer{

        public void response() {
            System.out.println("ObserverB 收到了");
        }
    }

    static class RealSubject extends Subject{

        void notifyAllOb() {
            for(Observer observer :list){
                observer.response();
            }
        }
    }

    public static void main(String[] args) {
        RealSubject realSubject = new RealSubject();
        ObserverA observerA = new ObserverA();
        ObserverB observerB = new ObserverB();
        realSubject.add(observerA);
        realSubject.add(observerB);
        realSubject.notifyAllOb();

    }
}
